home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16289 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.6 KB

  1. Path: news.salford.ac.uk!aber!not-for-mail
  2. From: pcg@aber.ac.uk (Piercarlo Grandi)
  3. Newsgroups: comp.object,comp.lang.eiffel,comp.lang.c++,comp.lang.beta,comp.lang.java,comp.lang.sather
  4. Subject: Re: What Should An Exception Handling Do? -- Clarification of rules
  5. Date: 09 Apr 1996 19:34:02 +0100
  6. Organization: Prifysgol Cymru, Aberystwyth
  7. Sender: pcg@osfb.aber.ac.uk
  8. Message-ID: <vwjk9zpckmd.fsf@osfb.aber.ac.uk>
  9. References: <4irn11$7ln@mimas.brunel.ac.uk> <4j03p4$fbt@hoho.quake.net>
  10.     <Doq3sv.MzA@research.att.com> <vwjg2ao6hqp.fsf@osfb.aber.ac.uk>
  11.     <4jqjvg$d9@glympton.airtime.co.uk>
  12. Reply-To: pcg@aber.ac.uk (Piercarlo Grandi)
  13. NNTP-Posting-Host: osfb.aber.ac.uk
  14. In-reply-to: wysiwyg@glympton.airtime.co.uk's message of 2 Apr 1996 08:11:44 +0100
  15. X-Newsreader: Gnus v5.0.15
  16.  
  17. >>> On 2 Apr 1996 08:11:44 +0100, wysiwyg@glympton.airtime.co.uk (Adam L
  18. >>> Rice) said:
  19.  
  20. pcg> The exception facility of e.g. C++ is merely a (rather ad-hoc and
  21. pcg> opaque) way of allowing the definition of what are in effect
  22. pcg> dynamically scoped procedure names, forcibly coupled with non-local
  23. pcg> control transfer.
  24.  
  25. wysiwyg> So, let me see if I've understood. What you're basically saying
  26. wysiwyg> is that instead of doing (in Java):
  27.  
  28. wysiwyg> try {
  29. wysiwyg>   sock = new Socket("wallawalla.mit.edu", 3456);
  30. wysiwyg> } catch (IOException e) {
  31. wysiwyg>   ... // handle the exception
  32. wysiwyg> }
  33. wysiwyg> ... // use the socket
  34.  
  35. wysiwyg> we should do something like:
  36.  
  37. wysiwyg> if (Socket.ConnectWillWork("wallawalla.mit.edu", 3456) {
  38. wysiwyg>   sock = new Socket("wallawalla.mit.edu", 3456);
  39. wysiwyg>   ... // use the socket
  40. wysiwyg> } else {
  41. wysiwyg>   ... // handle the exception
  42. wysiwyg> }
  43.  
  44. wysiwyg> Hmmmm... that's a lovely code style! I think we can call this techique
  45. wysiwyg> 'APIx2', for obvious reasons.
  46.  
  47. wysiwyg> But I'm not entirely clear, it might be you were saying we should do:
  48.  
  49. wysiwyg> sock = new Socket("wallawalla.mit.edu", 3456, new CantConnectHandler());
  50. wysiwyg> if (!CantConnectHandler.ConnectFailed) {
  51. wysiwyg>   ... // use the socket
  52. wysiwyg> }
  53.  
  54. You are making all this up, and stupidly too, for neither of the
  55. examples you provide are anywhere similar to the ones I have given (for
  56. the ones I have given are examples of how things would look like at the
  57. place where the exception is *raised*, while your examples are about the
  58. place where it is *detected*), and neither exhibits dynamically scoped
  59. procedure names or non local control transfers, which are part of my
  60. definition. Amazing ability to misread! (and yes, I apologize if what I
  61. wrote was opaque, but I think I was darn well unambiguous).
  62.  
  63. Perhaps you should reread more carefully what I have written, and
  64. perhaps refresh your memory on the notions of dynamic scoping and non
  65. local control transfer and how they look like.
  66.  
  67. To help you see the light, here is your example:
  68.  
  69. wysiwyg> try {
  70. wysiwyg>   sock = new Socket("wallawalla.mit.edu", 3456);
  71. wysiwyg> } catch (IOException e) {
  72. wysiwyg>   ... // handle the exception
  73. wysiwyg> }
  74. wysiwyg> ... // use the socket
  75.  
  76. rewritten using something less ``ad hoc'' than 'try', and with dynamic
  77. scoping and non local control transfer as I had written:
  78.  
  79.   jmp_buf bailOutTo;
  80.  
  81.   static void localHandler(IOException e)
  82.   {
  83.     .... // handle the ``exception''
  84.     longjmp(bailOutTo);
  85.   }
  86.  
  87.   void try()
  88.   {
  89.     dynamic void (*IOExceptionHandler)(IOException) = localHandler;
  90.  
  91.     sock = new Socket("wallawalla.mit.edu", 3456);
  92.     .... // use the socket
  93.   }
  94.  
  95. where presumably 'bailOutTo' is initialized to some recovery point
  96. somewhere.
  97.  
  98. 'dynamic' would be new storage class, much like 'extern', but with
  99. dynamic scoping within blocks.
  100.  
  101. The 'Socket' constructor would contain some bit of code like:
  102.  
  103.   dynamic void (*IOExceptionHandler)(IOException) = DefaultHandler;
  104.  
  105.   Socket::Socket(String s,int p)
  106.   {
  107.     ....
  108.     if (nogood)
  109.     {
  110.       // we call the user's routine, equivalent to a 'raise'
  111.       (*IOExceptionHandler)(new IOException(....));
  112.       abort();
  113.     }
  114.     ....
  115.   }
  116.  
  117. Naturally one would write it a bit better, but I have written it this
  118. way to show the parallel between the 'try' construction and the more
  119. linear one based on dynamic scoping and non local control transfers.
  120.  
  121. BTW, this style of ``exception handling'' has been rather the norm in
  122. Lisp/Scheme for the past few dozen years, and could be added to
  123. C++/Java/whatever in an afternoon (indeed 'dynamic' can be _simulated_
  124. tolerably well by a few lines of macros in C++).
  125.  
  126. Dynamic scoping is useful BTW not just for providing ``exception
  127. handling'', but for a number of other uses, all those like ``exception
  128. handling'' where code semantics must depend on the dynamic context and
  129. not the static context, for example when there are runtime
  130. parameters. Currently this must be inelegantly simulated with idioms
  131. like:
  132.  
  133.   SomeType GlobalParameter = defaultValue;
  134.  
  135.     ....
  136.  
  137.     {
  138.       SomeType savedGlobal = GlobalParameter;
  139.       GlobalParameter = localValue;
  140.  
  141.       ....
  142.  
  143.       GlobalParameter = savedGlobal;
  144.     }
  145.  
  146. which if of course nothing but a manual (and hazardous) implementation
  147. of dynamic scoping for GlobalParameter, and that is seen rather often in
  148. C and C++ programs, instead of:
  149.  
  150.   SomeType Parameter = defaultValue;
  151.     ....
  152.  
  153.     {
  154.       dynamic SomeType Parameter = localValue;
  155.  
  156.       ....
  157.     }
  158.  
  159. BTW, I really hate ``exception handling'', for in cases like say 0/0
  160. there is not exception, jsut the inability to prescribe a particular
  161. semantics statically. I think that something like ``deferred semantics''
  162. would be a much more descriptive title.
  163.  
  164. PS instead of 'dynamic' I think that 'extern auto' would be OK, and
  165. rather descriptive too, too bad that a C/C++ declaration can only have
  166. one storage class specifier.
  167.